package com.tf.xx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tf.xx.entity.EduSubject;
import com.tf.xx.entity.excel.SubjectData;
import com.tf.xx.entity.vo.OneSubject;
import com.tf.xx.entity.vo.TwoSubject;
import com.tf.xx.exception.MyException;
import com.tf.xx.listener.SubjectExcelListener;
import com.tf.xx.mapper.EduSubjectMapper;
import com.tf.xx.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有 一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);

        // 查询所有 二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);

        // 创建 list 集合，用于封装最终数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 外层循环：遍历所有 一级分类
        // 每次循环获取一个 一级分类对象，得到每个 一级分类对象后，获取每个 一级分类对象的所有二级分类集合
        for (int i = 0; i < oneSubjectsList.size(); i++) {
            EduSubject oSubject = oneSubjectsList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oSubject, oneSubject);
            finalSubjectList.add(oneSubject);

            // 在一级分类对象里添加所有的二级分类
            List<TwoSubject> finalTwoSubjects = new ArrayList<>();

            //遍历二级list集合，判断二级分类parentid和一级分类id是否一样
            for (int j = 0; j < twoSubjectsList.size(); j++) {
                EduSubject tSubject = twoSubjectsList.get(j);
                if (tSubject.getParentId().equals(oSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    finalTwoSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(finalTwoSubjects);
        }

        return finalSubjectList;
    }



    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            InputStream is =file.getInputStream();
            EasyExcel.read(is, SubjectData.class,(ReadListener) new SubjectExcelListener(eduSubjectService))
                    .sheet().doRead();
        }catch (Exception e){
            throw new MyException(20001,"excel文件读取异常!!");
        }
    }
}

